package web.service.impls;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import map.beans.EmpConfigManagerBean;
import map.beans.EmpMapVersionBean;
import map.beans.GeneralConfigBean;
import map.beans.GitVersionBean;
import map.beans.MapEngineRegistrationBean;
import map.beans.MapToolConfigBean;
import map.beans.OwfLibLocationBean;
import map.beans.WmsServerBean;
import map.utils.SpringBeansProvider;
import map.utils.json.InstanceCreators;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import web.dao.interfaces.MapConfigurationServiceInterface;
import web.service.interfaces.AdditionalConfigServiceInterface;
import web.service.interfaces.EngineRegistrationServiceInterface;
import web.service.interfaces.MapToolConfigServiceInterface;
import web.service.interfaces.OwfLibLocationServiceInterface;
import web.service.interfaces.WmsServiceInterface;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

@Repository("mapConfigService")
public class MapConfigurationServiceImpl implements MapConfigurationServiceInterface {
	public static final Logger LOGGER = Logger.getLogger(MapConfigurationServiceImpl.class.getName());
	
	@Autowired
	OwfLibLocationServiceInterface owfLibSvc;
	@Autowired
	WmsServiceInterface wmsSvc;
	@Autowired
	MapToolConfigServiceInterface toolsSvc;
	@Autowired
	EngineRegistrationServiceInterface engineRegSvc;
	@Autowired
	AdditionalConfigServiceInterface additConfigSvc;

	GitVersionBean empVersionInfo;
		
	public MapConfigurationServiceImpl() {
		super();
	}

	@Override
	public String getOwfJsLocation() {
		return owfLibSvc.getOwfLibLocationBean().getLibLocation();
	}
	
	@Override
	public boolean updateOwfJsLocation(OwfLibLocationBean owf) {		
		boolean isUpdated = false;
		if (owfLibSvc.update(owf) != null) {
			isUpdated = true;
		}
		return isUpdated;
	}

	@Override
	public List<WmsServerBean> getWmsServerUrls() {
		return  wmsSvc.findAll();
	}

	@Override
	public List<MapToolConfigBean> getMapTools() {
		return toolsSvc.findAll();
	}

	@Override
	public GitVersionBean getEmpVersionInformation() {
		empVersionInfo = SpringBeansProvider.getBean("gitVersionBean", GitVersionBean.class);
		return empVersionInfo;
	}

	@Override
	public List<MapEngineRegistrationBean> getMapEngines() {
		return engineRegSvc.findAll();
	}

	@Override
	public GeneralConfigBean getAdditionalConfig() {
		return additConfigSvc.getDefaultConfig();
	}

	@Override
	public EmpConfigManagerBean getMapConfig() {		
		return new EmpConfigManagerBean(getEmpVersionInformation(), getMapTools(), getWmsServerUrls(), 
				getMapEngines(), getOwfJsLocation(), getAdditionalConfig());
	}
	
	@Override
	public EmpConfigManagerBean parseInput(String content) {
		GsonBuilder gsonBuilder = new GsonBuilder();
		registerMapTypeAdapters(gsonBuilder);
		
		// instantiate gson builder
		Gson gson = gsonBuilder.setPrettyPrinting().create();
		
		EmpConfigManagerBean ecm = new EmpConfigManagerBean();
		ecm = gson.fromJson(content, EmpConfigManagerBean.class);
		System.out.println(ecm);
		return ecm;
	}

	private void registerMapTypeAdapters(GsonBuilder gsonBuilder) {
		gsonBuilder.registerTypeAdapter(EmpMapVersionBean.class, 		 new InstanceCreators.VersionInstanceCreator());
		gsonBuilder.registerTypeAdapter(MapToolConfigBean.class, 		 new InstanceCreators.MapToolInstanceCreator());		
		gsonBuilder.registerTypeAdapter(WmsServerBean.class, 			 new InstanceCreators.WmsServerInstanceCreator());
		gsonBuilder.registerTypeAdapter(MapEngineRegistrationBean.class, new InstanceCreators.MapEngineInstanceCreator());
		gsonBuilder.registerTypeAdapter(GeneralConfigBean.class, 		 new InstanceCreators.GeneralConfigInstanceCreator());
	}

	@Override
	public String fectchMapConfigAsJson() {
		// instantiate gson builder
		Gson gson = new GsonBuilder().create();
		// Convert to json string
		String json = gson.toJson(getMapConfig());

		return json;
	}

	@Override
	public boolean persistDataFile(String content) {
		boolean isSuccess = true;
		try {
			EmpConfigManagerBean ecm = parseInput(content);		
			
			ecm.getDeploymentInformation();
			additConfigSvc.update(ecm.getGeneralConfig());
			engineRegSvc.register(ecm.getMapEngines());
			toolsSvc.update(ecm.getMapTools());
			owfLibSvc.update(new OwfLibLocationBean(null, ecm.getOwfJsLocation()));
			wmsSvc.update(ecm.getWmsServerUrls());
		} catch (Exception ex) {
			LOGGER.log(Level.SEVERE, "Data import failed");
			isSuccess = false;
		}
		return isSuccess;
	}	

}
